<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoomFrame extends BaseModel
{
    protected $connection = 'api';
    protected $roomFrameTable = 'room_frame';
    protected $roomFrameLogTable = 'room_frame_log';
    protected $userTable = 'user';
    protected $room_table = 'room';//房间表
    protected $union_room = 'union_room';//公会房间
    protected $union = 'union';//公会表

    //房间边框新增
    public function roomFrameAdd($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/roomFrameAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //房间边框编辑
    public function roomFrameEdit($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/roomFrameEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //房间边框下发记录新增
    public function roomFrameUserAdd($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/roomFrameUserAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //房间边框下发记录编辑
    public function roomFrameUserEdit($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/roomFrameUserEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //房间边框下发记录删除
    public function roomFrameUserDel($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/roomFrameUserDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //房间边框列表
    public function roomFrameList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $frame_id = $request->input('frame_id', 0);
        $frame_name = $request->input('frame_name', '');

        $query = DB::connection($this->connection)->table($this->roomFrameTable)->where('is_delete', 0)->orderBy('id', 'desc');
        if(!empty($frame_id)) {
            $query = $query->where('id', $frame_id);
        }
        if(!empty($frame_name)) {
            $query = $query->where('frame_name', 'like', '%'.$frame_name.'%');
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);

        if(!empty($result['data'])) {
            foreach($result['data'] as $key=>$val) {
                $val['frame_img'] = build_image($val['frame_img']);

                $result['data'][$key] = $val;
            }
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //房间边框下发记录列表
    public function roomFrameLogList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $frame_id = $request->input('frame_id', 0);
        $frame_name = $request->input('frame_name', '');
        $room_id = $request->input('room_id', 0);
        $room_name = $request->input('room_name', '');

        $query = DB::connection($this->connection)->table($this->roomFrameLogTable.' as a')
            ->leftJoin($this->roomFrameTable.' as b', "a.frame_id", "=", "b.id")
            ->leftJoin($this->room_table.' as c', "a.room_id", "=", "c.room_id")
            ->leftJoin($this->union_room.' as d', "a.room_id", "=", "d.room_id")
            ->leftJoin($this->union.' as e', "d.un_id", "=", "e.id")
            ->orderBy('a.id', 'desc')
            ->select(['a.id', 'a.time_st', 'a.time_et', 'a.admin_id', 'a.updated_at'
            , 'a.frame_id', 'b.frame_name', 'b.frame_img', 'b.frame_desc'
            , 'a.room_id', 'c.room_uid', 'c.name as room_name', 'd.un_id as union_id', 'e.name as union_name'
            ])
            ->where('a.is_delete', 0);

        if(!empty($frame_id)) {
            $query = $query->where('a.frame_id', $frame_id);
        }
        if(!empty($frame_name)) {
            $query = $query->where('b.frame_name', 'like', '%'.$frame_name.'%');
        }
        if(!empty($room_id)) {
            $query = $query->where('a.room_id', $room_id);
        }
        if(!empty($room_name)) {
            $query = $query->where('c.name', 'like', '%'.$room_name.'%');
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);

        if(!empty($result['data'])) {
            $adminIds = array_column($result['data'], 'admin_id');
            $adminList = DB::table('users')->whereIn('id', $adminIds)->select(['id','name'])->get();
            $adminList = array_column(objToArray($adminList), null, 'id');

            foreach($result['data'] as $key=>$val) {
                $val['admin_name'] = empty($adminList[$val['admin_id']]['name']) ? '' : $adminList[$val['admin_id']]['name'];
                $val['frame_img'] = build_image($val['frame_img']);

                $result['data'][$key] = $val;
            }
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

}
